package com.company.bookstore.controller.order_module;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.order_module.Order;
import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.mapper.order_module.OrderMapper;
import com.company.bookstore.qo.order_module.OrderQo;
import com.company.bookstore.qo.user_admin_module.UserQo;
import com.company.bookstore.service.order_module.OrderService;
import com.company.bookstore.service.user_admin_module.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵云霞
 * @since 2020-09-08
 *
 * 订单超时 状态，创建时间
 * 返回前端限制时间，
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;
    @Resource
    UserService userService;
    @Resource
    OrderMapper orderMapper;


    //增加订单,下单操作
    @PostMapping("/save/{uid}")
    public MvcResult<Object> saveOrderQo(@PathVariable int uid, @RequestBody OrderQo[] orderQos) throws NoSuchMethodException, CustomException {

        try {
            List<Order> list = orderService.saveOrder(uid, orderQos);
            return MvcResult.builder().code(200).message("添加订单成功").object(list).build();
        } catch (Exception e) {
            e.printStackTrace();
            return MvcResult.builder().code(500).message("添加订单失败").build();
        }

    }

    //订单详情
    @GetMapping("/orderDetail/{oid}")
    public MvcResult<Object> getOrderDetail(@PathVariable int oid) throws NoSuchMethodException, CustomException {

        Order order = orderService.getOrderDetail(oid);
        if (order == null) {
            return MvcResult.builder().code(500).message("查询该订单失败").build();
        } else
            return MvcResult.builder().code(200).message("查询该订单成功")
                    .object(order)
                    .build();
    }

    //更新状态，发货完成
    @PutMapping("/update/status")
    public MvcResult<Object> updateStatus(@RequestBody Order[] orders) throws NoSuchMethodException, CustomException {

        List<Order> list = orderService.updateStatus(orders);
        if (list == null) {
            return MvcResult.builder().code(500).message("商品库存不足，支付失败").build();
        } else {
            return MvcResult.builder().code(200).message("修改订单状态成功").object(list).build();
        }
    }

    //修改订单地址
    @PutMapping("/update/{oid}")
    public MvcResult<Object> updateStatus(@PathVariable int oid, @RequestBody OrderQo orderQo) throws NoSuchMethodException, CustomException {

        try {
            Order order = orderService.updateDetails(orderQo, oid);
            return MvcResult.builder().code(200).message("修改订单成功").object(order).build();
        } catch (Exception e) {
            e.printStackTrace();
            return MvcResult.builder().code(500).message("修改订单失败").build();
        }
    }

    //获取订单list
    @GetMapping("/getOrderList/{uid}")
    public MvcResult<Object> getOrderList(@PathVariable int uid) throws NoSuchMethodException, CustomException {

        List<Order> list = orderService.getOrderByUid(uid);
        if (list == null) {
            return MvcResult.builder().code(500).message("查询该用户订单失败").build();
        } else
            return MvcResult.builder().code(200).message("查询该用户订单成功").object(list).build();
    }

    @DeleteMapping("/delete/{uid}/{oid}")
    public MvcResult<Object> deleteOrder(@PathVariable int uid, @PathVariable int oid) throws NoSuchMethodException, CustomException {

        if (!orderService.delete(uid, oid)) {
            return MvcResult.builder().code(500).message("删除该用户订单失败").build();
        } else {
            return MvcResult.builder()
                    .code(200).message("删除该用户订单成功")
                    .build();
        }
    }

    //分页查询
    @GetMapping("page/{current}/{size}")
    public MvcResult<Object> getOrderByPage(@PathVariable long current,
                                            @PathVariable long size)throws NoSuchMethodException, CustomException  {

        Page<Order> orderPage = orderService.getOrderPage(current, size);

        try{
            return MvcResult.builder().code(200)  .message("查询订单成功") .object(orderPage).build();
        }catch(Exception e){
            e.printStackTrace();
            return MvcResult.builder().code(500).message("查询订单失败").build();
        }

    }
    @PostMapping("/getTimeList")
    public MvcResult<Object> getTimeList(@RequestBody OrderQo orderQo)throws NoSuchMethodException, CustomException {

        try{
            Page<Order> page = orderService.fuzzySearchOrder(orderQo);
            return MvcResult.builder().code(200).message("查询时间段数据成功").object(page).build();
        }catch(Exception e){
            e.printStackTrace();
            return MvcResult.builder().code(500).message("查询时间段数据失败").build();
        }
    }

    @ApiOperation("用户列表接口")
    @GetMapping("/userList")
    @PreAuthorize(value = "hasRole('ROLE_ORDER')")
    public MvcResult<Object> getAll(@Nullable @RequestBody UserQo userQo){
        if (userQo == null){
            return MvcResult.builder().message("获取成功").code(200).object(userService.list()).build();
        }else {
            return MvcResult.builder().message("获取成功").code(200).object(userService.list()).build();
        }
    }
}

